package com.xiyoo.cw.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xiyoo.cw.domain.CheckinRecords;
import com.xiyoo.cw.domain.TaskCountVO;
import com.xiyoo.cw.domain.UserTaskDetailVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface TaskMapper extends BaseMapper<CheckinRecords> {

    @Select("SELECT any_value(u.nick_name) AS nickName, " +
            "SUM(cr.score) AS totalScore, " +
            "SUM(cr.carbon_reduction) AS totalCarbonReduction " +
            "FROM cw_user u " +
            "LEFT JOIN cw_checkin_records cr ON u.email = cr.email " +
            "WHERE cr.email = #{email}")
    UserTaskDetailVO selectUserTaskDetail(@Param("email") String email);

    @Select("SELECT task_id as taskId, COUNT(*) as count FROM cw_checkin_records WHERE email = #{email} GROUP BY task_id")
    List<TaskCountVO> selectUserTaskCount(@Param("email") String email);

}
